<?php
//$form_fields = get_fields('form', $module_fields);
$form_fields = get_form_order($module_fields);
$form_fields_types = get_names('type', $form_fields);
//			print_r($form_fields);
//			print_r($module_fields);
//		die;

//	print_r($module_fields);
$check_date_field = 0;

$module_name = $module_name;
$module_name_small = $module_name_small;

ob_start();
foreach ($form_fields as $each_field) {
	
	$required = "";
	if(in_array('require', $each_field['options']))
		$required = '<span class="required">*</span> ';
	
	switch($each_field['type']) {
		case 'date_added':
		case 'date_modified':
		case 'datetime':
		case 'date':
			$check_date_field = 1;
			echo "\r" . '			  <tr>' . "\n" .
'				<td>'.$required.'<?php echo $entry_' . $each_field['name'] . '; ?></td>' . "\n" .
'				<td><input type="text" name="' . $each_field['name'] . '" value="<?php echo $' . $each_field['name'] . '; ?>" class="date" />' . "\n" .
'				<?php if ($error_' . $each_field['name'] . ') { ?>' . "\n" .
'				<span class="error"><?php echo $error_' . $each_field['name'] . '; ?></span>' . "\n" .
'				<?php } ?></td>' . "\n" .
'			  </tr>' . "\n";				
		break;
		case 'text_email':
		case 'text':
			echo "\r" . '			  <tr>' . "\n" .
'				<td>'.$required.'<?php echo $entry_' . $each_field['name'] . '; ?></td>' . "\n" .
'				<td><input type="text" name="' . $each_field['name'] . '" value="<?php echo $' . $each_field['name'] . '; ?>" size="50" />' . "\n" .
'				<?php if ($error_' . $each_field['name'] . ') { ?>' . "\n" .
'				<span class="error"><?php echo $error_' . $each_field['name'] . '; ?></span>' . "\n" .
'				<?php } ?></td>' . "\n" .
'			  </tr>' . "\n";				
		break;
		case 'editor':
			echo "\r" . '			  <tr>' . "\n" .
'				<td>'.$required.'<?php echo $entry_' . $each_field['name'] . '; ?></td>' . "\n" .
'				<td><textarea id="editor_' . $each_field['name'] . '" name="' . $each_field['name'] . '" cols="50" rows="5"><?php echo $' . $each_field['name'] . '; ?></textarea>' . "\n" .
'				<?php if ($error_' . $each_field['name'] . ') { ?>' . "\n" .
'				<span class="error"><?php echo $error_' . $each_field['name'] . '; ?></span>' . "\n" .
'				<?php } ?></td>' . "\n" .
'			  </tr>' . "\n";				
		break;
		case 'textarea':
			echo "\r" . '			  <tr>' . "\n" .
'				<td>'.$required.'<?php echo $entry_' . $each_field['name'] . '; ?></td>' . "\n" .
'				<td><textarea name="' . $each_field['name'] . '" cols="50" rows="5"><?php echo $' . $each_field['name'] . '; ?></textarea>' . "\n" .
'				<?php if ($error_' . $each_field['name'] . ') { ?>' . "\n" .
'				<span class="error"><?php echo $error_' . $each_field['name'] . '; ?></span>' . "\n" .
'				<?php } ?></td>' . "\n" .
'			  </tr>' . "\n";				
		break;
		case 'select':
			$field_name = $each_field['name'];
			if(preg_match('@_id@', $each_field['name'])) {
				$field_name = str_replace('_id', '', $each_field['name']);	
			}
		echo "\r" . '			  <tr>' . "\n" .
'				<td>'.$required.'<?php echo $entry_' . $each_field['name'] . '; ?></td>' . "\n" .
'				<td><select name="' . $each_field['name'] . '">' . "\n" .
'				<option value="*"></option>' . "\n" .
'				<?php foreach ($' . $each_field['name'] . 's as $each_' . $each_field['name'] . ') { ?>' . "\n" .
'				<?php if ($each_' . $each_field['name'] . '[\'' . $field_name . '_id\'] == $' . $each_field['name'] . ') { ?>' . "\n" .
'					<option value="<?php echo $each_' . $each_field['name'] . '[\'' . $field_name . '_id\']; ?>" selected="selected"><?php echo $each_' . $each_field['name'] . '[\'name\']; ?></option>' . "\n" .
'				<?php } else { ?>' . "\n" .
'					<option value="<?php echo $each_' . $each_field['name'] . '[\'' . $field_name . '_id\']; ?>"><?php echo $each_' . $each_field['name'] . '[\'name\']; ?></option>' . "\n" .
'				<?php } ?>' . "\n" .
'				<?php } ?>' . "\n" .
'				</select>' . "\n" .
'				<?php if ($error_' . $each_field['name'] . ') { ?>' . "\n" .
'				<span class="error"><?php echo $error_' . $each_field['name'] . '; ?></span>' . "\n" .
'				<?php } ?></td>' . "\n" .
'				</tr>' . "\n";
		
		break;
		case 'status':
		
		echo "\r" . '			  <tr>' . "\n" .
'				<td>'.$required.'<?php echo $entry_' . $each_field['name'] . '; ?></td>' . "\n" .
'				<td><?php foreach ($' . $each_field['name'] . 's as $each_' . $each_field['name'] . ') { ?>' . "\n" .
'				<?php if ($each_' . $each_field['name'] . '[\'' . $each_field['name'] . '_id\'] == $' . $each_field['name'] . ') { ?>' . "\n" .
'					<lable><input type="radio" name="' . $each_field['name'] . '" value="<?php echo $each_' . $each_field['name'] . '[\'' . $each_field['name'] . '_id\']; ?>" checked="checked"><?php echo $each_' . $each_field['name'] . '[\'name\']; ?></lable>' . "\n" .
'				<?php } else { ?>' . "\n" .
'					<lable><input type="radio" name="' . $each_field['name'] . '" value="<?php echo $each_' . $each_field['name'] . '[\'' . $each_field['name'] . '_id\']; ?>"><?php echo $each_' . $each_field['name'] . '[\'name\']; ?></lable>' . "\n" .
'				<?php } ?>' . "\n" .
'				<?php } ?>' . "\n" .
'				<?php if ($error_' . $each_field['name'] . ') { ?>' . "\n" .
'				<span class="error"><?php echo $error_' . $each_field['name'] . '; ?></span>' . "\n" .
'				<?php } ?></td>' . "\n" .
'				</tr>' . "\n";				
		break;				
		default :
			echo "\r" . '			  <tr>' . "\n" .
'				<td>'.$required.'<?php echo $entry_' . $each_field['name'] . '; ?></td>' . "\n" .
'				<td><input type="text" name="' . $each_field['name'] . '" value="<?php echo $' . $each_field['name'] . '; ?>" />' . "\n" .
'				<?php if ($error_' . $each_field['name'] . ') { ?>' . "\n" .
'				<span class="error"><?php echo $error_' . $each_field['name'] . '; ?></span>' . "\n" .
'				<?php } ?></td>' . "\n" .
'			  </tr>' . "\n";	 
		break;
	}
}
$form_input_fields = ob_get_clean();

ob_start();
if(in_array('editor', $form_fields_types)) {
	echo "\r" . '<script type="text/javascript" src="view/javascript/ckeditor/ckeditor.js"></script>' . "\n" .
		'<script type="text/javascript"><!--' . "\n";
	
	foreach($form_fields as $each_field) {
		if($each_field['type'] == 'editor')
		echo 'CKEDITOR.replace(\'editor_' . $each_field['name'] . '\', {' . "\n" .
		'filebrowserBrowseUrl: \'index.php?route=common/filemanager&token=<?php echo $token; ?>\',' . "\n" .
		'filebrowserImageBrowseUrl: \'index.php?route=common/filemanager&token=<?php echo $token; ?>\',' . "\n" .
		'filebrowserFlashBrowseUrl: \'index.php?route=common/filemanager&token=<?php echo $token; ?>\',' . "\n" .
		'filebrowserUploadUrl: \'index.php?route=common/filemanager&token=<?php echo $token; ?>\',' . "\n" .
		'filebrowserImageUploadUrl: \'index.php?route=common/filemanager&token=<?php echo $token; ?>\',' . "\n" .
		'filebrowserFlashUploadUrl: \'index.php?route=common/filemanager&token=<?php echo $token; ?>\'' . "\n" .
		'});' . "\n\n";	
	}
	
	echo '//--></script>'. "\n";
}

if($check_date_field) {
	echo '<script type="text/javascript"><!--' . "\n" .
		 '	$(document).ready(function() {' . "\n" .
		 '		$(\'.date\').datepicker({dateFormat: \'yy-mm-dd\'});' . "\n" .
		 '	});' . "\n" .
		 '//--></script>' . "\n";
}
$java_scripts = ob_get_clean();

$maches_arr = array (
	'/@module_name@/',
	'/@module_name_small@/',
	'/@form_input_fields@/',
	'/@java_scripts@/',
	
);

$replace_arr = array (
	$module_name,
	$module_name_small,
	$form_input_fields,
	$java_scripts,
	
);

//	preg_match_all('/@([^@]+)@/', $list_file_contents, $maches);
//	sort($maches['1']);
//	print_r(array_unique($maches['1']));

$form_file_contents = preg_replace($maches_arr, $replace_arr, $form_file_contents);
